<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
      //map 循环遍历数组，每次遍历都执行回调函数
      //把自己的map方法挂载在原型对象上
      //准备一个形参接受回调函数
        Array.prototype.myMap = function (fn) {
            const arr = []
            for (let i = 0; i < this.length; i++) {
                arr[i] = fn(this[i], i, this)
            }
            return arr
        }

        const arr = [1, 2, 3, 4]

        const newArr = arr.myMap((item, index, array) => item)
        

        Array.prototype.myFilter = function (fn) {
            const arr = []
            for (let i = 0; i < this.length; i++) {
                
                if (fn(this[i], i, this)) {
                    arr[i] = this[i]
                }
            }
            return arr
        }
        const newNewArr = arr.myFilter((item, index, array) => item > 2)
        console.log(newNewArr)
    </script>
</body>

</html>